package com.aha.dataanalyzeandview.config;


import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池
 */
public class MyThreadPoolExecutor {

    private static ThreadPoolExecutor threadPoolExecutor;

    public static ThreadPoolExecutor buildThreadPool(){
        return threadPoolExecutor;
    }

    // 不允许new这个对象
    private MyThreadPoolExecutor(){}

    static {
         // 创建线程工厂
        ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("thread-pool-[%d]").build();

        // 创建线程池
         threadPoolExecutor = new ThreadPoolExecutor(5,32,60L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<>(1024),
                tf,
                new ThreadPoolExecutor.AbortPolicy());


    }


    public static void shutdown(){
        threadPoolExecutor.shutdown();
    }

}


